﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace Addins.Ftp
{
    internal static class StreamFactory
    {
        private readonly static object myLock = new object();
        private static Dictionary<Guid, MemoryStream> Streams = new Dictionary<Guid, MemoryStream>();

        public static MemoryStream CreateStream()
        {
            lock (myLock)
            {
                try
                {
                    Guid _guid = Guid.NewGuid();
                    while (Streams.ContainsKey(_guid))
                    {
                        _guid = Guid.NewGuid();
                    }

                    Streams.Add(_guid, new MemoryStream());

                    return Streams[_guid];
                }
                catch
                {
                    return null;
                }
            }
        }

        public static IEnumerable ReadAllStream()
        {
            foreach (KeyValuePair<Guid, MemoryStream> stream in Streams)
                yield return stream.Value;
        }
    }
}
